package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.car.param.*;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.CarBrandDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.car.form.InsertCarBrandForm;
import com.woniu108.car.form.UpdateCarBrandImageForm;
import com.woniu108.car.model.CarBrand;
import com.woniu108.car.service.CarBrandService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆品牌表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carBrand")
@Api(tags = "汽车品牌管理")
public class CarBrandController {
    @Resource
    private CarBrandService carBrandService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询所有汽车品牌
     *
     * @return CarBrandDto
     */
    @GetMapping("/findAllCarBrand.api")
    @ApiOperation("查询所有汽车品牌")
    public Result<List<CarBrandDto>> findAllCarBrand() {
        List<CarBrand> list = carBrandService.findAllCarBrand();
        List<CarBrandDto> dtos = BeanUtil.copyToList(list, CarBrandDto.class);
        return Result.newListInstance(CarBrandDto.class).setData(dtos).setMsg("查询所有汽车品牌成功");
    }

    /**
     * 查询所有正常汽车品牌
     *
     * @return CarBrandDto
     */
    @GetMapping("/findAllNormalCarBrand")
    @ApiOperation("查询所有正常汽车品牌")
    public Result<List<CarBrandDto>> findAllNormalCarBrand() {
        List<CarBrand> list = carBrandService.findAllNormalCarBrand();
        List<CarBrandDto> dtos = BeanUtil.copyToList(list, CarBrandDto.class);
        return Result.newListInstance(CarBrandDto.class).setData(dtos).setMsg("查询所有汽车品牌成功");
    }

    /**
     * 新增汽车品牌
     *
     * @param form  名字，logo
     * @param token 管理员token
     * @return boolean值
     */
    @PostMapping("/insertCarBrand.api")
    @ApiOperation("新增汽车品牌")
    @AdminHistoryLog
    public Result<Boolean> insertCarBrand(InsertCarBrandForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertCarBrandParam param = BeanUtil.toBean(form, InsertCarBrandParam.class);
        param.setAdminId(adminId);
        Boolean bool = carBrandService.insertCarBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增汽车品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增汽车品牌失败").setData(false).setCode(500);
    }

    /**
     * 删除汽车品牌
     *
     * @param brandIds 品牌id集合
     * @param token    管理员token
     * @return boolean值
     */
    @PostMapping("/deleteCarBrand.api")
    @ApiOperation("删除汽车品牌")
    @AdminHistoryLog
    @ApiImplicitParam(name="brandIds",value = "品牌id集合")
    public Result<Boolean> deleteCarBrand(@RequestBody List<Integer> brandIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteCarBrandParam param = new DeleteCarBrandParam();
        param.setBrandIds(brandIds);
        param.setAdminId(adminId);
        Boolean bool = carBrandService.deleteCarBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除汽车品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除汽车品牌失败").setData(false).setCode(500);
    }


    /**
     * 禁用汽车品牌
     *
     * @param brandIds 品牌id集合
     * @param token    管理员token
     * @return boolean值
     */
    @PostMapping("/banCarBrand.api")
    @ApiOperation("禁用汽车品牌")@AdminHistoryLog
    @ApiImplicitParam(name="brandIds",value = "品牌id集合")
    public Result<Boolean> banCarBrand(@RequestBody List<Integer> brandIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanCarBrandParam param = new BanCarBrandParam();
        param.setBrandIds(brandIds);
        param.setAdminId(adminId);
        Boolean bool = carBrandService.banCarBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用汽车品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用汽车品牌失败").setData(false).setCode(500);
    }

    /**
     * 解锁汽车品牌
     *
     * @param brandIds 品牌id集合
     * @param token    管理员token
     * @return boolean值
     */
    @PostMapping("/unlockCarBrand.api")
    @ApiOperation("解锁汽车品牌")@AdminHistoryLog
    @ApiImplicitParam(name="brandIds",value = "品牌id集合")
    public Result<Boolean> unlockCarBrand(@RequestBody List<Integer> brandIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockCarBrandParam param = new UnlockCarBrandParam();
        param.setBrandIds(brandIds);
        param.setAdminId(adminId);
        Boolean bool = carBrandService.unlockCarBrand(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁汽车品牌成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁汽车品牌失败").setData(false).setCode(500);
    }

    /**
     * 修改汽车品牌
     *
     * @param form  品牌id，图片文件
     * @param token 管理员token
     * @return boolean值
     */
    @PostMapping("/updateCarBrandImage.api")
    @ApiOperation("修改汽车品牌图片")@AdminHistoryLog
    public Result<Boolean> updateCarBrandImage(UpdateCarBrandImageForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateCarBrandImageParam param = BeanUtil.toBean(form, UpdateCarBrandImageParam.class);
        param.setAdminId(adminId);
        Boolean bool = carBrandService.updateCarBrandImage(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改汽车品牌图片成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改汽车品牌图片失败").setData(false).setCode(500);

    }
}

